<?php

namespace App\Models\Order;

use App\Models\Fee\FeeType;
use App\Models\Manger\Manger;
use App\Models\Settle\SettleCompany;
use Illuminate\Database\Eloquent\Model;

/***
 *   原来的销售费用付款逻辑， 每条其他费用只能付款一次。
 *   目前废弃
 *
 *
 *
 */
class Saleorderfee extends Model
{
    protected $table = 'sale_order_otherfee';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];



    public static function getList($request, $memberinfo, $isgetall)
    {
        $manger_id = $memberinfo['id'] ?? 0;
        $cpid = $memberinfo['cpid'] ?? 0;
        $sys_dpm_id = $memberinfo['sys_dpm_id'] ?? 0;

        $query = self::with(['pminfo','feeinfo','paybank'])->orderBy('pay_at', 'desc');

        if($memberinfo['utype'] == 7){
            $query->where('pay_mid', '=', $manger_id);
        }



        $query->where('cpid', $cpid);

        $ft_id = $request->ft_id;
        if ($ft_id > 0) {
            $query->where('fee_type_id', $ft_id);
        }
        $jj_mark = $request->jj_mark;
        if ($jj_mark) {
            $query->where('jj_mark', $jj_mark);
        }

        $fee_min=intval(floatval($request->fee_min)*100);
        $fee_max=intval(floatval($request->fee_max)*100);
        if($fee_min>0 && $fee_max>0){
            $query->whereBetween('fee_num',[$fee_min,$fee_max]);
        }

        $custom_name = $request->custom_name;
        if ($custom_name) {
            $query->where('custom_name', 'like','%'.$custom_name.'%');
        }





        $settlement_company = $request->settlement_company;
        if ($settlement_company) {
            $query->where('pay_bank', $settlement_company);
        }

        $status = $request->status;
        if ($status > 0) {
            $query->where('status', $status);
        }


        $pmname = $request->pmname;
        if ($pmname) {
            $query->whereHas('pminfo', function ($query) use ($pmname) {
                $query->where('realname', $pmname);
            });
        }





        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }
    public static function getListBycn($request, $memberinfo, $isgetall)
    {
        $manger_id = $memberinfo['id'] ?? 0;
        $cpid = $memberinfo['cpid'] ?? 0;
        $sys_dpm_id = $memberinfo['sys_dpm_id'] ?? 0;

        $query = self::with(['pminfo','feeinfo','paybank'])->orderBy('pay_at', 'desc');

        // $query->where('manger_id', '=', $manger_id);

        $query->where('cpid', $cpid);

        $ft_id = $request->ft_id;
        if ($ft_id > 0) {
            $query->where('fee_type_id', $ft_id);
        }
        $jj_mark = $request->jj_mark;
        if ($jj_mark) {
            $query->where('jj_mark', $jj_mark);
        }

        $fee_min=intval(floatval($request->fee_min)*100);
        $fee_max=intval(floatval($request->fee_max)*100);
        if($fee_min>0 && $fee_max>0){
            $query->whereBetween('fee_num',[$fee_min,$fee_max]);
        }


        $custom_name = $request->custom_name;
        if ($custom_name) {
            $query->where('custom_name', 'like','%'.$custom_name.'%');
        }

        $query->where('status', 3);
        $pmname = $request->pmname;
        if ($pmname) {
            $query->whereHas('pminfo', function ($query) use ($pmname) {
                $query->where('realname', 'like', '%'.$pmname.'%');
            });
        }



        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }





    public static function getListByTs($request, $memberinfo, $isgetall)
    {
        $manger_id = $memberinfo['id'] ?? 0;
        $cpid = $memberinfo['cpid'] ?? 0;
        $sys_dpm_id = $memberinfo['sys_dpm_id'] ?? 0;

        $query = self::with(['feeinfo','paybank'])->orderBy('pay_at', 'desc');


        $query->where('ts_mid', $manger_id);

        $query->where('cpid', $cpid);

        $ft_id = $request->ft_id;
        if ($ft_id > 0) {
            $query->where('fee_type_id', $ft_id);
        }


        $ft_name = $request->ft_name;
        if ($ft_name!='') {
            $query->where('fee_name', 'like','%'.$ft_name.'%');
        }


        $jj_mark = $request->jj_mark;
        if ($jj_mark) {
            $query->where('jj_mark', $jj_mark);
        }

        $fee_min=intval(floatval($request->fee_min)*100);
        $fee_max=intval(floatval($request->fee_max)*100);

        if($fee_min>0 && $fee_max>0){
            $query->whereBetween('fee_num',[$fee_min,$fee_max]);
        }

        $custom_name = $request->custom_name;
        if ($custom_name) {
            $query->where('custom_name', 'like','%'.$custom_name.'%');
        }

        // $status = $request->status;
        // if ($status > 0) {
        //     $query->where('status', $status);
        // }





        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }


    public static function getListByCp($request, $memberinfo, $isgetall)
    {
        $manger_id = $memberinfo['id'] ?? 0;
        $cpid = $memberinfo['cpid'] ?? 0;
        $sys_dpm_id = $memberinfo['sys_dpm_id'] ?? 0;

        $query = self::with(['feeinfo','paybank'])->orderBy('pay_at', 'desc');


        $query->where('cp_mid', $manger_id);

        $query->where('cpid', $cpid);

        $ft_id = $request->ft_id;
        if ($ft_id > 0) {
            $query->where('fee_type_id', $ft_id);
        }

        $ft_name = $request->ft_name;
        if ($ft_name!='') {
            $query->where('fee_name', 'like','%'.$ft_name.'%');
        }

        $jj_mark = $request->jj_mark;
        if ($jj_mark) {
            $query->where('jj_mark', $jj_mark);
        }

        $fee_min=intval(floatval($request->fee_min)*100);
        $fee_max=intval(floatval($request->fee_max)*100);
        if($fee_min>0 && $fee_max>0){
            $query->whereBetween('fee_num',[$fee_min,$fee_max]);
        }

        $custom_name = $request->custom_name;
        if ($custom_name) {
            $query->where('custom_name', 'like','%'.$custom_name.'%');
        }

        // $status = $request->status;
        // if ($status > 0) {
        //     $query->where('status', $status);
        // }





        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }


    public function tsinfo(){
        return $this->belongsTo(Manger::class,'ts_mid','id');
    }

    public function cpinfo(){
        return $this->belongsTo(Manger::class,'cp_mid','id');
    }

    public function pminfo(){
        return $this->belongsTo(Manger::class,'pay_mid','id');

    }



    public function feeinfo(){
        return $this->belongsTo(FeeType::class,'fee_id','id');

    }

    public function paybank(){
        return $this->belongsTo(SettleCompany::class,'pay_bank','id');
    }

    public function approve(){
        return $this->hasMany(SaleorderOtherfeePayApprove::class,'otherfee_id','id');
    }
}
